package com.os.sys.common.configuration;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2017/1/11.
 */
@Controller
public class MyErrorController implements ErrorController {

    private static final String ERROR_PATH = "/error";

    @RequestMapping(value=ERROR_PATH)
    public String handleError(HttpServletRequest request, HttpServletResponse response){


            String reason = "";
            Enumeration<String> em = request.getAttributeNames();
            while (em.hasMoreElements()) {
                String key=em.nextElement();
                String errmsg=request.getAttribute(key).toString();
                if(key.contains("error") || key.contains("Exception") ||errmsg.contains("error") || errmsg.contains("Exception"))
                    reason += key+":"+errmsg + "\r\n";
            }

            Logger.getLogger("").warning(reason);

        return "errorpage/error404";
        //return "redirect:/errorpage/error404?abc=1";

    }

    @Override
    public String getErrorPath() {
        // TODO Auto-generated method stub
        return ERROR_PATH;
    }
}
